package com.lanchetech.user.service.impl;


import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.common.enums.DeletedEnum;
import com.lanchetech.common.enums.JobStatusEnum;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.dao.JobApplyMapper;
import com.lanchetech.dao.JobMapper;
import com.lanchetech.entity.Job;
import com.lanchetech.entity.JobApply;
import com.lanchetech.entity.User;
import com.lanchetech.user.service.JobApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class JobApplyServiceImpl implements JobApplyService {

    @Autowired
    JobApplyMapper jobApplyMapper;

    @Autowired
    JobMapper jobMapper;

    @Override
    public BaseResp editJobApply(JobApply jobApply) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Job job = jobMapper.selectByPrimaryKey(jobApply.getJobId());
        if(jobApply.getId() == null){
            if(job.getDeleted().equals(DeletedEnum.DELETED.getStatus()) || job.getStatus().equals(JobStatusEnum.CLOSE.getStatus())){
                return new BaseResp(ResultCode.FAIL);
            }
            jobApply.setCreatedAt(new Date());
            jobApply.setUpdatedAt(new Date());
            jobApply.setUserId(user.getId());
            jobApplyMapper.insert(jobApply);
            return new BaseResp();
        }
        return new BaseResp(ResultCode.FAIL);
    }
}
